package com.zxy.supplier_system.server.service

import com.zxy.supplier_system.server.controller.GoodsBrandGroupController
import com.zxy.supplier_system.server.entity.GoodsBrandGroup
import com.zxy.supplier_system.server.repository.GoodsBrandGroupRepository
import com.zxy.supplier_system.server.repository.GoodsBrandRepository
import com.zxy.supplier_system.server.repository.SystemServiceRepository
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.utils.orElse404
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class GoodsBrandGroupService(
    private val goodsBrandGroupRepository: GoodsBrandGroupRepository,
    private val systemServiceService: SystemServiceService,
    private val goodsBrandRepository: GoodsBrandRepository,
    private val employeeOperateRecordService: EmployeeOperateRecordService,
) {


    @Transactional(readOnly = true)
    fun queryGoodsBrandGroups(
        pageable: Pageable,
        queryGoodsBrandGroupsRequest: GoodsBrandGroupController.QueryGoodsBrandGroupsRequest
    ): Page<GoodsBrandGroupController.QueryGoodsBrandGroupsResponse> {
        return this.goodsBrandGroupRepository.findAll(
            GoodsBrandGroupRepository.Specifications.fromQueryGoodsBrandGroupsRequest(queryGoodsBrandGroupsRequest).and(
                SystemServiceRepository.Specifications.systemServiceEquals(
                    { root -> root },
                    systemServiceService.getCurrentSystemService()
                )

            ), pageable
        ).map {
            GoodsBrandGroupController.QueryGoodsBrandGroupsResponse(
                it.name,
                it.id,
                this.goodsBrandRepository.countAllByGroup(it)
            )
        }
    }

    @Transactional
    fun updateGoodsBrandGroup(id: Int, updateGoodsBrandGroupRequest: GoodsBrandGroupController.UpdateGoodsBrandGroupRequest) {
        val data = this.goodsBrandGroupRepository.findByIdOrNull(id).orElse404()
        data.name = updateGoodsBrandGroupRequest.name
        this.goodsBrandGroupRepository.save(data)
        this.employeeOperateRecordService.updateGoodsBrandGroup(data)
    }

    @Transactional
    fun createGoodsBrandGroup(updateGoodsBrandGroupRequest: GoodsBrandGroupController.UpdateGoodsBrandGroupRequest) {
        val goodsBrandGroup = this.goodsBrandGroupRepository.save(GoodsBrandGroup().apply {
            name = updateGoodsBrandGroupRequest.name
            systemService = systemServiceService.getCurrentSystemService()
        })
        this.employeeOperateRecordService.createGoodsBrandGroup(goodsBrandGroup)
    }

    @Transactional
    fun deleteGoodsBrandGroup(id: Int) {
        this.goodsBrandGroupRepository.deleteById(id)
    }


    fun getAllGoodsBrandGroups(): List<IntIdAndName> {
        return this.goodsBrandGroupRepository.findAllBySystemService(systemServiceService.getCurrentSystemService()).map {
            IntIdAndName(it.id, it.name)
        }
    }


}
